package com.yhsimon.controller;


import com.yhsimon.entity.Account;
import com.yhsimon.entity.Admin;
import com.yhsimon.entity.User;
import com.yhsimon.feign.AccountFeign;
import com.yhsimon.utils.ReflectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/account")
public class AccountHandler {

    @Autowired
    private AccountFeign accountFeign;

    @GetMapping("/redirect/{location}")
    public String index(@PathVariable("location") String location) {
        return location;
    }

    @PostMapping("/login")
    public Object login(@RequestParam("userName") String userName, @RequestParam("password") String password, @RequestParam("type") String type, HttpSession session, Model model) throws InterruptedException {
        Account account = accountFeign.login(userName, password, type);

        String result = null;
        System.out.println("account:"+account);
        if (account == null) {
            model.addAttribute("msg", "error");
            result = "login";
        } else {

            switch (type) {
                case "user":
                    User user = convertUser(account);
                    System.out.println("user----------"+account.toString());
                    session.setAttribute("user", user);
                    model.addAttribute("msg", "success");
                    result = "redirect:/account/redirect/home";
                    break;
                case "admin":
                    System.out.println("admin--------"+account.toString());
                    Admin admin = convertAdmin(account);
                    session.setAttribute("admin", admin);
                    model.addAttribute("msg", "success");
                    result = "redirect:/account/redirect/main";
                    break;
                default:
                    break;
            }
        }
        return result;
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "login";
    }

    private User convertUser(Account account) {
        User user = new User();
        user.setUserName(ReflectUtils.getFieldValue(account, "userName") + "");

        user.setPassword(ReflectUtils.getFieldValue(account, "password") + "");
        user.setGender(ReflectUtils.getFieldValue(account, "gender") + "");
        user.setId((Integer) ReflectUtils.getFieldValue(account, "id"));
        user.setNickName(ReflectUtils.getFieldValue(account, "nickName") + "");
        user.setRegisterDate((Date) (ReflectUtils.getFieldValue(account, "registerDate")));
        user.setTelephone(ReflectUtils.getFieldValue(account, "telephone") + "");
        return user;
    }

    private Admin convertAdmin(Account account) {
        Admin admin = new Admin();
        admin.setUserName(ReflectUtils.getFieldValue(account, "userName") + "");
        admin.setPassword(ReflectUtils.getFieldValue(account, "password") + "");
        admin.setId((Integer) ReflectUtils.getFieldValue(account, "id"));
        return admin;
    }


}
